// index.ts
import { service } from './service'
import { config } from './config'

const { defaultHeaders } = config

interface RequestOptions {
  url: string
  method?: 'get' | 'post' | 'delete' | 'put'
  params?: object
  data?: object
  headersType?: string
  responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'
}

const request = (option: RequestOptions) => {
  const { url, method = 'get', params, data, headersType, responseType } = option
  return service({
    url,
    method,
    params,
    data,
    responseType,
    headers: {
      'Content-Type': headersType || defaultHeaders,
    },
  })
}

export default {
  get: (option: Omit<RequestOptions, 'method'>) => request({ method: 'get', ...option }),
  post: (option: Omit<RequestOptions, 'method'>) => request({ method: 'post', ...option }),
  delete: (option: Omit<RequestOptions, 'method'>) => request({ method: 'delete', ...option }),
  put: (option: Omit<RequestOptions, 'method'>) => request({ method: 'put', ...option }),
}
